<?php

namespace app\admin\controller;

use app\common\controller\Backend;
//use app\common\library\Sms as Smslib;
use app\common\model\User;
use think\Db;
use think\Exception;

/**
 * 手机短信接口
 */
class Sms extends Backend
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    /**
     * 发送验证码
     *
     * @param string $mobile 手机号
     * @param string $event 事件名称
     */
    public function send()
    {
        $mobile = $this->request->request("mobile");
        $event = $this->request->request("event");
        $pagetype = $this->request->request("pagetype")?:'admin';
        $event = $event ? $event : 'login';

        if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('手机号不正确'));
        }
        $type = 1;
        if($event == 'login'){
            if($pagetype == 'web'){
                $admin = Db::name('admin')->where('org_cate_id', '>', 0)->where('mobile', $mobile)->find();
                if(empty($admin)){
                    $this->error(__('手机号【'.$mobile.'】未注册或者不是单位平台手机号！'));
                }
            }else{
                $admin = Db::name('admin')->where('org_cate_id', 0)->where('mobile', $mobile)->find();
                if(empty($admin)){
                    $this->error(__('手机号【'.$mobile.'】未注册或者不是总平台手机号！'));
                }
                $type = 2;
            }
        }

//        $last = \app\common\model\Sms::where(['mobile' => $mobile, 'event' => $event])->order('id', 'DESC')->find();
//        if ($last && time() - $last['createtime'] < 60) {
//            $this->error(__('发送频繁'));
//        }
//        $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
//        if ($ipSendTotal >= 5) {
//            $this->error(__('发送频繁'));
//        }
        $code = mt_rand(1000, 9999);
        $time = time();
        $ip = request()->ip();

        $sms = \app\common\model\Sms::create(['event' => $event, 'mobile' => $mobile, 'code' => $code,'type'=>$type, 'content' => '您的验证码为：'.$code.'，您正在进行后台登录操作，如非本人操作，请忽略本短信！', 'ip' => $ip, 'createtime' => $time]);
        $info['code'] = $code;
        $alisms = new \addons\alisms\library\Alisms();
        $config = config('config')['alisms'];
        $res = $alisms->mobile($mobile)
//            ->template('SMS_176528942')
            ->template($config['template']['code2'])
            ->sign($config['sign'])
            ->param($info)
            ->send();
        if ($res) {
            $this->success(__('发送成功'));
        } else {
            $sms->delete();
            $this->error(__('发送失败'));
        }

    }

    /**
     * 检测验证码
     *
     * @param string $mobile 手机号
     * @param string $event 事件名称
     * @param string $captcha 验证码
     */
    public function check()
    {
        $mobile = $this->request->request("mobile");
        $event = $this->request->request("event");
        $event = $event ? $event : 'login';
        $captcha = $this->request->request("captcha");

        if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('手机号不正确'));
        }
        $ret =  \app\common\library\Sms::check($mobile, $captcha, $event);
//        $ret = Smslib::check($mobile, $captcha, $event);
        if ($ret) {
            $this->success(__('成功'));
        } else {
            $this->error(__('验证码不正确'));
        }
    }
}
